// $Id: collapse.js,v 1.0 2010/02/17 20:16:25 goba Exp $

RaysnWeb.behaviors.collapse = function (context) {
	$('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
		var fieldset = $(this.parentNode);
		var legend = $(this).text();

		$(this).empty().append($('<a href="#">'+ legend +'</a>').click(function() {
			var fieldset = $(this).parents('fieldset:first')[0];
			// Don't animate multiple times
			if (!fieldset.animating) {
				fieldset.animating = true;
				RaysnWeb.toggleFieldset(fieldset);
			}
			return false;
		}))
		.after($(fieldset.children(':not(legend)')).addClass('fieldset-wrapper'))
		.addClass('collapse-processed');
	});
};

/**
 * Toggle the visibility of a fieldset using smooth animations
 */
RaysnWeb.toggleFieldset = function(fieldset) {
	if ($(fieldset).is('.collapsed')) {
		// Action div containers are processed separately because of a IE bug
		// that alters the default submit button behavior.
		var content = $('> .fieldset-wrapper', fieldset);
		$(fieldset).removeClass('collapsed');
		content.hide();
		content.slideDown( {
			duration: 'fast',
			easing: 'linear',
			complete: function() {
				RaysnWeb.ScrollIntoView(this.parentNode);
				this.parentNode.animating = false;
			},
			step: function() {
				// Scroll the fieldset into view
				RaysnWeb.ScrollIntoView(this.parentNode);
			}
		});
	}
	else {
		$('> .fieldset-wrapper', fieldset).slideUp('fast', function() {
			$(this.parentNode).addClass('collapsed');
			this.parentNode.animating = false;
		});
	}
};
